﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;

namespace mylist.infraestrucure.nhibernate
{
    public class SessionManager
    {
        public static readonly Configuration Configuration;
        public static readonly ISessionFactory SessionFactory;

        static SessionManager()
        {
            var configuration = new Configuration();
            configuration.DataBaseIntegration(c =>
                {
                    c.Dialect<MsSql2008Dialect>();
                    c.ConnectionString = @"Server=db003.appharbor.net;Database=db4037;User ID=db4037;Password=5V3YfZewTNSyqq2BHXUR4YcdGzGXJmPdcyx8D5kdKG7ugxSW6i2pVqXfSXrMsoUF;";
                    c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                    c.SchemaAction = SchemaAutoAction.Create;
                });

            var mapper = new ModelMapper();
            mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
            HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
            configuration.AddMapping(domainMapping);

            SessionFactory = configuration.BuildSessionFactory();
        }

        public static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }
}
